<template>
  <div>
    <el-divider content-position="left">应用列表</el-divider>

    <el-form :inline="true">
      <el-form-item label="应用名称">
        <el-input v-model="searchForm.appName" clearable></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchForm.status">
          <el-option value="" label="全部"></el-option>
          <el-option value="0" label="正常"></el-option>
          <el-option value="1" label="下线"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="init">查询</el-button>
        <el-button icon="el-icon-plus" type="primary" @click="addOrUpdate(undefined)">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)" border>
      <el-table-column label="应用ID" align="center" prop="id"></el-table-column>
      <el-table-column label="应用名称" align="center" prop="appName"></el-table-column>
      <el-table-column label="跳转路径" prop="redirectUrl" align="center">
        <template slot-scope="scope">
          <el-link :href="scope.row.redirectUrl" target="_blank" type="primary">{{ scope.row.redirectUrl }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status==0">正常</el-tag>
          <el-tag v-if="scope.row.status==1" type="danger">已下线</el-tag>
        </template>
      </el-table-column>
      <el-table-column>
        <template slot-scope="scope">
          <el-button icon="el-icon-edit" size="mini" circle type="primary" @click="addOrUpdate(scope.row)"></el-button>
          <el-button icon="el-icon-delete" size="mini" circle type="danger" @click="remove(scope.row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[5,10]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.length">
      </el-pagination>
    </div>
    <transition name="el-fade-in-linear">
      <app-add-or-update-modal
        ref="AppAddOrUpdateModal" v-if="AppAddOrUpdateModalVisible">
      </app-add-or-update-modal>
    </transition>
  </div>
</template>

<script>
import {deleteApp, getList} from '@/api/app'
import AppAddOrUpdateModal from "./AppAddOrUpdateModal";

export default {
  name: "app-index",
  components: {AppAddOrUpdateModal},
  mounted() {
    this.init()
  },
  data() {
    return {
      currentPage: 1,
      pagesize: 5,
      total: 0,
      searchForm: {
        appName: '',
        status: ''
      },
      AppAddOrUpdateModalVisible: false,
      tableData: []
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pagesize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    init() {
      getList({
        appName: this.searchForm.appName,
        status: this.searchForm.status
      }).then((r) => {
        this.tableData = r
      })
    },
    addOrUpdate(row) {
      this.AppAddOrUpdateModalVisible = true
      this.$nextTick(() => {
        this.$refs.AppAddOrUpdateModal.init(row)
      })
    },
    remove(id) {
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteApp(id).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功'
          });
          this.init()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
}
</script>

<style scoped>

</style>
